import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class calculator {
	private static JTextField []text = new JTextField[5];
	
    public static void main(String[] args) {
        JFrame frame = new JFrame("Easy Calculator");
        frame.setSize(295, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);
        frame.setVisible(true);
    }
    
    private static class ButtonHandle implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		JButton button = (JButton) e.getSource();
    		if (button.getText() != "OK") {
    			text[1].setText(button.getText());
    		}
    		else {
    			calculate();
    		}
    	}
    }
    
    private static void calculate() {
    	try {
    		double a = Double.valueOf(text[0].getText());
        	double b = Double.valueOf(text[2].getText());
        	String s = text[1].getText();
        	if (s.equals("+")) {
        		text[4].setText(String.valueOf(a + b));
        	}
        	else if (s.equals("-")) {
        		text[4].setText(String.valueOf(a - b));
        	}
        	else if (s.equals("*")) {
        		text[4].setText(String.valueOf(a * b));
        	}
        	else if (s.equals("/")) {
        		text[4].setText(String.valueOf(a / b));
        	}
    	}
    	catch(Exception e){
    		text[4].setText("error");
    	}
    }
    
    private static void placeComponents(JPanel panel) {
    	Font font = new Font("tom's new romen", Font.ITALIC, 11);
    	panel.setLayout(null);
    	
    	for (int i = 0; i < 5; i++) {
    		text[i] = new JTextField(10);
    		text[i].setHorizontalAlignment(JTextField.CENTER);
    		text[i].setFont(font);
    		text[i].setBounds(i * 55 + 5,4,50,50);
    		if (i == 1 || i == 3 || i == 4) {
    			text[i].setEditable(false);
    			if (i == 3)
    				text[i].setText("=");
    		}
    		panel.add(text[i]);
    	}
    	JButton []button = new JButton[5];
    	String s[] = {"+","-","*","/","OK"};
    	for (int i = 0; i < 5; i++) {
    		button[i] = new JButton(s[i]);
    		button[i].setHorizontalAlignment(JButton.CENTER);
    		button[i].setFont(font);
    		button[i].setBounds(i * 55 + 5,58,50,50);
    		button[i].addActionListener(new ButtonHandle());
    		panel.add(button[i]);
    	}
    }
    
    
}
